<?php

/**
 * Created by PhpStorm.
 * User: YF
 * Date: 16/8/25
 * Time: 上午12:05
 */
namespace Conf;

use Core\Component\Spl\SplArray;

class Config
{
    private static $instance;
    protected $conf;

    function __construct()
    {
        $conf = $this->sysConf() + $this->userConf();
        $this->conf = new SplArray($conf);
    }

    static function getInstance()
    {
        if (!isset(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }

    function getConf($keyPath)
    {
        return $this->conf->get($keyPath);
    }

    /*
            * 在server启动以后，无法动态的去添加，修改配置信息（进程数据独立）
    */
    function setConf($keyPath, $data)
    {
        $this->conf->set($keyPath, $data);
    }

    private function sysConf()
    {
        return array(
            "SERVER" => array(
                "LISTEN" => "0.0.0.0",
                "SERVER_NAME" => "",
                "PORT" => 9501,
                "RUN_MODE" => SWOOLE_PROCESS,//不建议更改此项
                "SERVER_TYPE" => \Core\Swoole\Config::SERVER_TYPE_WEB,//
                'SOCKET_TYPE' => SWOOLE_TCP,//当SERVER_TYPE为SERVER_TYPE_SERVER模式时有效
                "CONFIG" => array(
                    'task_worker_num' => 8, //异步任务进程
                    "task_max_request" => 10,
                    'max_request' => 5000,//强烈建议设置此配置项
                    'worker_num' => 8,
                ),
            ),
            "DEBUG" => array(
                "LOG" => true,
                "DISPLAY_ERROR" => true,
                "ENABLE" => true,
            ),
            "CONTROLLER_POOL" => true//web或web socket模式有效
        );
    }

    private function userConf()
    {
        return array(
            'MYSQL' => array(
                'db_type' => 'mysql',
                'host' => '127.0.0.1',
                'port' => '3306',
                'user' => 'root',
                'password' => '123456',
                'db_name' => 'ueb_omslabel'
            ),
            'REDIS' => array(
                'HOST' => '127.0.0.1',
                'PORT' => '6379'
            )
        );
    }
}